/*
 * DefaultListModel.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing.list;

import java.util.ArrayList;

/**
 * Default List Model.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class DefaultListModel extends AbstractListModel {

  // TODO: finish

// Private Fields. -------------------------------------------------------------

  private final ArrayList data;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new DefaultListModel object.
   */
  public DefaultListModel() {
    data = new ArrayList();
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Adds the specified element to the list.
   *
   * @param element the element
   */
  public void add(Object element) {
    int index = data.size();
    data.add(element);
    fireIntervalAdded(this, index, index);
  }

  /**
   * Inserts the specified element into the list.
   *
   * @param index the element position
   * @param element the element
   */
  public void add(int index, Object element) {
    data.add(index, element);
    fireIntervalAdded(this, index, index);
  }

  /**
   * Removes all elements.
   */
  public void clear() {
    int lastIndex = data.size() - 1;
    data.clear();
    if (lastIndex >= 0) {
      fireIntervalRemoved(this, 0, lastIndex);
    }
  }

  /**
   * Returns <tt>true</tt> if the model contains the specified element.
   *
   * @param elem the element to search for.
   *
   * @return <tt>true</tt> if the model contains the specified element
   */
  public boolean contains(Object elem) {
    return data.contains(elem);
  }

// ListModel Interface Implementation. -----------------------------------------

  /**
   * {@inheritDoc}
   */
  public int getSize() {
    return data.size();
  }

  /**
   * {@inheritDoc}
   */
  public Object getElementAt(int index) {
    return data.get(index);
  }

}
